#include "wsserver.hpp"

#define HOST "127.0.0.1"
#define USERNAME "root"
#define PASSWORD "wyn15598303669"
#define DBNAME "gobang"
#define PORT 3306

void test_mysql()
{
    MYSQL* mysql = mysql_util::mysql_create(HOST, USERNAME, PASSWORD, DBNAME, PORT);
    //进行MySQL数据库操作功能的验证
    // const char* sql = "insert into stu values (null, '张三', 18, 99, 101, 111);";
    const char* sql = "update stu set ch=ch+30 where sn=3;";
    // const char* sql = "delete from stu where sn=1;";
    // const char* sql = "select * from stu;";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if(ret == false) return;

    mysql_util::mysql_destroy(mysql);
}
void test_json()
{
    Json::Value root;
    std::string body;
    root["姓名"] = "张三";   // grep -R 'Value(' /usr/include/jsoncpp/json/value.h递归搜索
    root["年龄"] = 18;       // 单参数构造+拷贝构造
    root["成绩"].append(98); // root["成绩"]先构造一个Json::Value对象出来，然后使用append函数来添加数组元素
    root["成绩"].append(88.5);
    root["成绩"].append(78.5);
    json_util::serialize(root, body);
    DLOG("%s", body.c_str());

    Json::Value val;
    json_util::deserialize(body, val);
    std::cout << "姓名: " << root["姓名"].asCString() << std::endl;
    std::cout << "年龄: " << root["年龄"].asInt() << std::endl;
    int sz = root["成绩"].size();
    for (int i = 0; i < sz; i++)
        std::cout << "成绩: " << root["成绩"][i].asFloat() << std::endl;
}
void test_split()
{
    std::vector<std::string> res;
    string_util::split(",,,123.....,345,,,,567,,", ",", res);
    for(auto s : res) std::cout << s << " ";
    printf("\n");
}
void test_read()
{
    std::string filename = "./logger.hpp";
    std::string body;
    file_util::read(filename, body);
    std::cout << body << std::endl;
}
void test_db()
{
    user_table ut(HOST, USERNAME, PASSWORD, DBNAME, PORT);
    Json::Value user;
    user["username"] = "rygttm";
    // user["password"] = "1234445";
    bool ret = ut.insert(user);
    // std::string body;
    // json_util::serialize(user, body);
    // DLOG("%s", body.c_str());
}
void test_online()
{
    online_manager om;
    wsserver_t::connection_ptr conn;//一个空的智能指针对象
    int uid = 2;
    om.enter_game_hall(uid, conn);
    if(om.is_in_game_hall(uid))
    {
        DLOG("user in game hall");
    }
    else 
    {
        DLOG("user not in game hall");
    }
    om.exit_game_hall(uid);
    if(om.is_in_game_hall(uid))
    {
        DLOG("user in game hall");
    } 
    else
    {
        DLOG("user not in game hall");
    }
}
int main()
{
    // test_mysql();
    // test_json();
    // test_split();
    // test_read();
    // test_db();
    // test_online();
    // online_manager om;
    // user_table ut(HOST, USERNAME, PASSWORD, DBNAME, PORT);
    // room r(10, &ut, &om);
    // room_manager rm(&ut, &om);
    // room_ptr rp = rm.create_room(10, 20);
    // rm.destroy_room(rp->get_room_id());
    // wsserver_t svr;
    // session_manager sm(&svr);
    // match_manager mc(&rm, &ut, &om);

    gobang_server svr(HOST, USERNAME, PASSWORD, DBNAME, PORT);
    svr.run(8080);


    return 0;
}